import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('@/views/Login/Login')
const Home = () => import('@/views/Home/Home');
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/Login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:Home
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由跳转前验证token
router.beforeEach((to,from,next) => {
  if(to.path === '/login'){
    next();
    return;
  }
  const data = sessionStorage.getItem('token');
  if(!data){
    this.$message({
      message:'请先登录！',
      type:'warning'
    })
    next('/login');
    return;
  }
  next()

})
export default router
